<?php


  $db = null; //php没有这么声明全局变量的 ,, 需要global

   function InitDb()
   {
     $dbtype = 'mysql';				//数据库类型
	$dbhost = 'http://112.121.164.117:999';			//主机名
	$dbhost=localhost;  //只能是这个localhost
	$dbname = 'sqlhelloworld';			//数据库名
     $dbuser = 'helloworld';		//数据库用户名
	$dbpassword = 'helloworld';	//数据库密码


	// 数据库连接
	//$db = new PDO("$dbtype:host=$dbhost;dbname=$dbname", $dbuser, $dbpassword, array(PDO::ATTR_PERSISTENT=>TRUE));
	$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpassword, array(PDO::ATTR_PERSISTENT=>TRUE));
	//设置使用UTF8字符集
	$db->query("set names UTF8");
	return $db;
   }


	function  addOperationRecord($postObj,$operation="imei",$memo="memo" )
	{
	     $db=InitDb();
	     $from = $postObj->FromUserName;

         $date = date("Y-m-d H:i:s",time());  //"2017-07-23 09:23:03";
         $make_day = date("Y-m-d",time());


	     $res = $db->exec("INSERT INTO hmkj SET `user_id`='$from',operation='$operation',make_time='$date',make_day='$make_day',memo='$memo'");
	     $db = null;

	     //ignore_user_abort(false); // 如果设置为 true，则忽略与用户的断开，如果设置为 false，会导致脚本停止运行。

         //include_once ("test.php");
	     //test4($postObj, $res);
	}



	function  checkAccessCount($postObj,$operation="imei",$memo='memo')
	{
	     $db=InitDb();
	     $from = $postObj->FromUserName;

         //$date = date("Y-m-d H:i:s",time());  //"2017-07-23 09:23:03";
         $make_day = date("Y-m-d",time());

	     $res = $db->query("select * from hmkj where user_id='$from' and make_day='$make_day'  and  ( operation='imei'  or   operation='sn')")->fetchAll(PDO::FETCH_ASSOC);



	    $count =count($res);//$res.rowCount();//
	    $db = null;
	    if( $count>=3)  return false ;
	    else return true;


	    // include_once ("test.php");
	    //test4($postObj, $count);

    }

   function  GetPeerIp()
   {
        $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
        $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
        return $user_IP;
   }

   /*

   foreach($res as $row){
 echo $row['username'].'<br/>';
 */
?>